from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .base import TimedBaseModel


class Cart(TimedBaseModel):
    __tablename__ = "cart"
    __mapper_args__ = {"eager_defaults": True}

    id: Mapped[int] = mapped_column(primary_key=True)
    products = relationship("CartProduct", back_populates="cart", cascade="all,delete")
    user = relationship("User", back_populates="cart")
    user_id: Mapped[int] = mapped_column(ForeignKey("user.id"), unique=True)

    def __repr__(self):
        return f"<Корзина пользователя: {self.user_id}"

    def __str__(self):
        return self.__repr__()
